package com.baitiaojun.user.api.impl;

import com.alibaba.fastjson.JSONObject;
import com.baitiaojun.api.annotation.JSR303Verify;
import com.baitiaojun.api.annotation.UserAuthoritiesAccess;
import com.baitiaojun.api.domain.user.User;
import com.baitiaojun.api.domain.user.UserCoin;
import com.baitiaojun.api.domain.user.UserInfo;
import com.baitiaojun.api.exception.ConditionException;
import com.baitiaojun.api.util.AESUtil;
import com.baitiaojun.api.util.ConstantUtil;
import com.baitiaojun.api.util.FileUtil;
import com.baitiaojun.api.vo.JsonRes;
import com.baitiaojun.user.api.UserApi;
import com.baitiaojun.user.service.UserCoinService;
import com.baitiaojun.user.service.UserInfoService;
import com.baitiaojun.user.support.FastdfsSupport;
import com.baitiaojun.user.support.UserSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @类名 UserController
 * @描述:获取公钥、注册新用户、登录、获取个人信息、更新个人系统信息、更新个人基本信息、注销
 * @作者 白条君
 * @创建日期 2022/10/25 19:40
 * @版本 1.0
 */
@RestController
public class UserApiImpl implements UserApi {

    @Resource
    private UserSupport userSupport;

    @Resource
    private UserInfoService userInfoService;

    @Resource
    private HttpServletRequest request;

    @Resource
    private HttpServletResponse response;

    @Autowired
    private FastdfsSupport fastdfsSupport;

    @Autowired
    private UserCoinService userCoinService;

    @UserAuthoritiesAccess(
            accessRole = {ConstantUtil.ROLE_LV0
                    ,ConstantUtil.ROLE_LV1
                    ,ConstantUtil.ROLE_LV2
                    ,ConstantUtil.ROLE_LV3
                    ,ConstantUtil.ROLE_LV4
                    ,ConstantUtil.ROLE_LV5
                    ,ConstantUtil.ROLE_LV6
            })
    @Override
    public JsonRes getCurrentUserInfo() throws Exception {
        User user = JSONObject.parseObject(JSONObject.toJSONString(userSupport.getCurrentUser(request)), User.class);
        String token = userSupport.getToken(request);
        user = userInfoService.getCurrentUserInfo(user, token);
        response.setHeader("Authorization", userSupport.getToken(request));
        return JsonRes.builder().success().data(user);
    }

    @Override
    public JsonRes getOtherUserInfo(Long id) throws Exception {
        Long userId = null;
        try {
            userId = userSupport.getCurrentUserId(request);
        } catch (Exception ignored) {}
        User user = userInfoService.getOtherUser(id, userId);
        return JsonRes.builder().success().data(user);
    }

  @UserAuthoritiesAccess(
            accessRole = {ConstantUtil.ROLE_LV0
                    ,ConstantUtil.ROLE_LV1
                    ,ConstantUtil.ROLE_LV2
                    ,ConstantUtil.ROLE_LV3
                    ,ConstantUtil.ROLE_LV4
                    ,ConstantUtil.ROLE_LV5
                    ,ConstantUtil.ROLE_LV6
            })
    @JSR303Verify
    @Override
    public JsonRes updateUser(@Valid User user, BindingResult result) throws Exception {
        Long userId = userSupport.getCurrentUserId(request);
        String token = userSupport.getToken(request);
        user.setId(userId);
        user.setPassword(user.getPassword());
        userInfoService.updateUser(user, token);
        response.setHeader("Authorization", token);
        return JsonRes.builder().success();
    }

    @UserAuthoritiesAccess(
            accessRole = {ConstantUtil.ROLE_LV0
                    ,ConstantUtil.ROLE_LV1
                    ,ConstantUtil.ROLE_LV2
                    ,ConstantUtil.ROLE_LV3
                    ,ConstantUtil.ROLE_LV4
                    ,ConstantUtil.ROLE_LV5
                    ,ConstantUtil.ROLE_LV6
            })
    @JSR303Verify
    @Override
    public JsonRes updateUserInfo(@Valid UserInfo userInfo, BindingResult result) throws Exception {
        Long userId = userSupport.getCurrentUserId(request);
        userInfo.setUserid(userId);
        String token = userSupport.getToken(request);
        userInfoService.updateUserInfo(userInfo, token);
        response.setHeader("Authorization", token);
        return JsonRes.builder().success();
    }

    @Override
    public JsonRes getUserCoin(Long userId) {
        String token = userSupport.getToken(request);
        Integer userCoin = userCoinService.getUserCoin(userId, token);
        response.setHeader("Authorization", token);
        return JsonRes.builder().data(userCoin);
    }

    @Override
    public JsonRes updateUserCoin(UserCoin userCoin, String token) throws ConditionException {
        userCoinService.updateUserCoin(userCoin, token);
        return JsonRes.builder().success();
    }

    @Override
    public List<UserInfo> batchGetUserInfoByUserIdsSet(Set<Long> userIds) throws Exception{
        Long userId = null;
        try {
            userId = userSupport.getCurrentUserId(request);
        } catch (Exception e) {}
        return userInfoService.getUserInfoListByUserId(userIds, userId);
    }

    @Override
    public UserInfo getUserInfo(Long userId) throws Exception {
        return userInfoService.getUserInfoByUserId(userId);
    }

    @Override
    public void getUserAvatar(String filePath) throws Exception {
        filePath = filePath.replace(" ", "+");
        AESUtil aesUtil = AESUtil.lazyGetAesUtil();
        String decryptFilePath = aesUtil.decrypt(filePath);
        Map<String, String> filePathMap = FileUtil.splitFilePath(decryptFilePath);
        fastdfsSupport.getUserAvatar(filePathMap.get("group"), request, response, filePathMap.get("filePath"));
    }

    @UserAuthoritiesAccess(
            accessRole = {ConstantUtil.ROLE_LV0
                    ,ConstantUtil.ROLE_LV1
                    ,ConstantUtil.ROLE_LV2
                    ,ConstantUtil.ROLE_LV3
                    ,ConstantUtil.ROLE_LV4
                    ,ConstantUtil.ROLE_LV5
                    ,ConstantUtil.ROLE_LV6
            })
    @Override
    public JsonRes uploadUserAvatar(MultipartFile file) throws Exception {
        String storePath = fastdfsSupport.uploadUserAvatar(file);
        return JsonRes.builder().success().data(storePath);
    }
}